﻿import getNetWork from '../../libs/getNetWork.js';
import portlet from '../../components/portlet.vue';

export default {
    components:{
        portlet,
    },
    data() {
        return {
            total:0,
            page:1,
            rows:10,
            name:'',
            tableData: [],
            data2: [],
            addData:{
                user_id:"",
                arroperate:[]
            },
            checked:null,
            loading:false,
            loading1:true,
            showCheck:true,
            dialogFormVisible:false,
            dialogMenuOperate:false,
            total1:0,
            page1:1,
            rows1:40,
            item:[],
            form: {
              name: '',
              reallyname:'',
              password:'',
              phone:'',
            },
            dialogForm1Visible:false,
            form1: {
              name: '',
              oldname:'',
              id:'',
              password:''
            },
            dialogForm2Visible:false,
            tableData1: [],
             fullscreenLoading: false,
             operateArray:[],
             terrList:[],
             terrshow:[],
             defaultProps: {
              children: 'children',
              label: 'menu_name'
            },
        };
    },

    mounted:function() {

            this.getData();
        
        
    },
    methods:{
        getSavedState(){
                let stateS=sessionStorage.getItem('myInfor');
                if(stateS!==null){
                    this.store=JSON.parse(stateS);
                    console.log(this.store);
                }
        },
        getData(){
                this.loading=true;
                let that=this;
                let data={
                    url:that.getNetDataAddress+'/user/selectUserByPage',
                    data:{
                        page:that.page,
                        rows:that.rows,
                        username:that.name
                        
                    },
                    successCallback(res){
                        that.tableData=res.body.dataObj.rows;
                        that.total=res.body.dataObj.records;
                        that.loading=false;
                    },
                    defaultCallback(){
                        that.loading=false;
                    }
                }
                getNetWork.post(this,data);

        },
         rolechange(event,checked){
                 let that=this;
                 let data={
                 url:that.getNetDataAddress+'/user/setRoleChecked',
                 data:{
                    roleid:event,
                    checked:checked,
                    userid:that.checked.id,
                 },
                 successCallback(res){
                        
                  },
                  defaultCallback(){
                        
                   }
                }
                getNetWork.post(this,data);
        },
        getData1(){
                this.fullscreenLoading = true;
                 let that=this;
                 let data={
                 url:that.getNetDataAddress+'/user/selectRoleByPage',
                 data:{
                    userid:that.checked.id,
                    page:that.page1,
                    rows:that.rows1,
                 },
                 successCallback(res){
                        that.item=res.body.dataObj.rows;
                        that.total1=res.body.dataObj.records;
                        that.fullscreenLoading = false;
                  },
                  defaultCallback(){
                        
                   }
                }
                getNetWork.post(this,data);

        },
        changeData(){
            this.loading=true;
                let that=this;
                let data={
                    url:that.getNetDataAddress+'/user/updateUser',
                    data:{
                        page:that.page,
                        rows:that.rows,
                        rolename:that.name,
                    },
                    successCallback(res){
                        that.tableData=res.body.dataObj.rows;
                        that.total=res.body.dataObj.records;
                        that.loading=false;
                    },
                    defaultCallback(){
                        that.loading=false;
                    }
                }
                getNetWork.post(this,data);
                this.getData();
        },
        handleTableChange(val) {

            this.checked=val;
            //console.log('sss'+this.checked.id)
            this.form1.name=val.login_code;
            this.form1.id=val.id;
            this.form1.oldname=val.login_code;
            
        },
        handleSizeChange(val) {
            this.rows=val;
            this.getData();
        },
        handleCurrentChange(val) {
            this.page = val;
            this.getData();
        },
        handleSizeChange1(val) {
            this.rows1=val;
            this.getData1();
        },
        handleCurrentChange1(val) {
            this.page1 = val;
            this.getData1();
        },
        handleCheckChange(data, checked, indeterminate) {//勾选树的时候 加入terrList
        
            if(data.level==2){
                if(checked){
                    this.terrList.push(data.id)
                    
                }else{
                    this.terrList.splice(this.terrList.indexOf(data.id),1);
                }
            }
        //console.log(this.terrList);
        },
        search(){
            this.getData();
        },
        add(){
            let that=this;
            if(that.form.name==null||that.form.name==""){
                    that.$message.error('请输入姓名！');
                    return;
            }
            
            this.dialogFormVisible=false;
            let data={
                url:that.getNetDataAddress+'/user/addUser',
                data:{
                    name:that.form.name,
                    password:that.form.password,

                },
                successCallback(res){
                    if(res.body.dataObj&&res.body.dataObj.exist=='true'){
                        that.$message('用户名重复！');
                    }else{
                        that.$message("添加成功");
                        that.form.name="";
                        that.form.reallyname="";
                        that.getData();
                    }
                }
            }
            getNetWork.post(this,data); 
            
        },
        delet(){
            if(this.checked==null){
                this.$message({
                    type: 'info',
                    message: '请选择要删除的行！'
                  });
                return;
            }

            let that=this;
            console.log(this.checked);
            this.$confirm('此操作将永久删除用户'+that.checked.login_code+', 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
             }).then(() => {
                 let data={
                     url:that.getNetDataAddress+'/user/deleteByName',
                     data:{
                        id:that.checked.id,
                      },
                    successCallback(res){      
                        that.$message('删除成功！');
                        that.getData();      
                    }
                }
                getNetWork.post(this,data);
             }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
            
        },
        update(){
            if(this.checked==null){
                this.$message({
                    type: 'info',
                    message: '请选择要修改的行！'
                  });
                return;
            }
            this.dialogForm1Visible = true
        }, 
        usermenu(){
            if(this.checked==null){
                this.$message({
                    type: 'info',
                    message: '请选择要修改的行！'
                  });
                return;
            }
            this.addMenuOperate(this.checked.id)
            
        }, 
      
        setrole(){

            let that=this;
            if(this.checked==null){
                this.$message({
                    type: 'info',
                    message: '请选择要修改的行！'
                  });
                return;
            }        
            this.getData1();
            this.dialogForm2Visible = true;
        },
        update1(){
            
            let that=this;
            if(that.form1.name==null||that.form1.name==""){
                    
                    that.$message.error('请输入姓名！');
                    return;
            }
            
            this.dialogForm1Visible=false;
            let data={
                url:that.getNetDataAddress+'/user/updateUser',
                data:{
                    name:that.form1.name,
                    id:that.form1.id,
                    oldname:that.form1.oldname,
                    password:that.form1.password
                },
                successCallback(res){
                    if(res.body.dataObj&&res.body.dataObj.exist=='true'){
                        that.$message('用户名重复！');
                    }else{
                        that.form1.password="";
                       that.$message('修改成功！');
                       that.getData();
                    }
                    
                }
            }
            getNetWork.post(this,data);
        },
        getTerrShow(data2){//terr的回显
            let temp=[];
            for(let i in data2){
                if(data2[i].children.length!=0){
                    data2[i].children.forEach((val)=>{
                        if(val.bb!=0){
                            temp.push(val.id);
                        }
                    })
                }
            }

            return temp;
        },
        addMenuOperate(row){//打开菜单操作页面
            this.data2=[];
            //console.log('gg')
            //console.log(row);
            this.addData.user_id=row;
            this.dialogMenuOperate=true;
            this.loading1=true;
            //this.loading=false;
                let that=this;
                let data={
                    url:that.getNetDataAddress+'/userMenu/findMenuOperateAll',
                    data:{
                       user_id:that.addData.user_id,
                    },
                    successCallback(res){
                        that.data2=res.body.dataArr;
                        that.terrList=that.getTerrShow(that.data2);
                        /*that.$nextTick(()=>{
                            that.getCheckList(that.data2);
                        })*/
                        
                    },
                    defaultCallback(){
                        that.loading1=false;
                    }
                }
                getNetWork.post(this,data);
        },
        addRoleMenu(){//添加角色菜单 角色操作中间表
            
            let that=this;
            //that.mycheckList();
                let data={
                    url:that.getNetDataAddress+'/userMenu/updateRoleMenu',
                    data:{ 
                       user_id:that.addData.user_id, 
                       terrList:that.terrList.toString(),
                       /*resultList:that.resultList.toString(),*/
                    },
                    successCallback(res){
                        that.$message('添加成功！');
                        that.dialogMenuOperate=false;
                        /*that.getUserOperator(that);*///修改完角色的操作后，根据角色修改用户的新操作权限，存入全局变量
                    },
                    defaultCallback(){
                        that.loading=false;
                    }
                }
                getNetWork.post(this,data);
        }

    }
};
